<?php

class Public_Post_CommentAction extends BlogPublicBaseAction
{
    
    /**
     * Process HTTP GET request. Display error 404 because GET request is not
     * allowed on this kind of URL.
     *
     * @param AgaviRequestDataHolder $rd
     * @return mixed
     */
    public function executeRead(AgaviRequestDataHolder $rd)
    {
        return array('Public', 'Error404Success');
    }
    
    
    /**
     * Process HTTP POST request. Submit comment.
     *
     * @param AgaviRequestDataHolder $rd
     * @return mixed
     */
    public function executeWrite(AgaviRequestDataHolder $rd)
    {
        $params = $rd->getParameters();
        $user = $this->getContext()->getUser();
		/* @var $posts_model Public_PostsModel */
        $posts_model = $this->getContext()->getModel('Posts', 'Public');
        /* @var $comments_model Public_CommentsModel */
        $comments_model = $this->getContext()->getModel('Comments', 'Public');
        
        $post = $posts_model->getPost($params['post_id']);
        if (!$post->is_commenting_enabled) {
            $this->setAttribute('comments_allowed', FALSE);
            return 'Error';
        }
        
        $parent_comment = $comments_model->getBlogPostComment($post, $params['f285a77b1dbe20195abbed303ba917b9']);
        
        $display_name = $email = null;
        $sys_user = null;
        $approved = FALSE;
        $is_owner = FALSE;
        if (!$user->isAuthenticated()) {
            // retreive values from request
            $display_name = $params['91324832772cfd4c610d642464643020'];
            $email = $params['625f80171a8c3bc19df088e1b36f723d'];
            
            // remember values
            $response = $this->getContext()->getController()->getGlobalResponse();
            $ro = $this->getContext()->getRouting();
            $path = $ro->gen('root');
            $time = 60*60*24*365;
            $response->setCookie('commenter_display_name', $display_name, $time, $path);
            $response->setCookie('commenter_email', $email, $time, $path);
        } else {
            /* @var $users_model Admin_UsersModel */
            $users_model = $this->getContext()->getModel('Users', 'Admin');
            /* @var $sys_user User */
            if (is_null($user->getAttribute('user_id'))) {
                // i.e. this is an owner
                $is_owner = TRUE;
                $approved = TRUE;
            } else {
                $sys_user = $users_model->getUser($user->getAttribute('user_id'));
                $approved = $user->hasCredential('comments.no_moderation');
            }
        }
        
        $lo_website = strtolower($params['958d848c2e1d82c9ce35daa1af42e675']);
        if ($lo_website != '' && strpos($lo_website, 'http://') !== 0 && strpos($lo_website, 'https://') !== 0) {
            $params['958d848c2e1d82c9ce35daa1af42e675'] = 'http://'.$params['958d848c2e1d82c9ce35daa1af42e675'];
        }
        
        $comment = $comments_model->addComment($post, $sys_user, $is_owner, $parent_comment, 
            $display_name, $email, $params['958d848c2e1d82c9ce35daa1af42e675'], $params['510601484237f985720e4fb59f1a6a47'], $approved);
        
        // clear some cache entries
        $posts_model->clearPostCache($post);
        
        // send notifications
        /* @var $notifications_model Public_NotificationsModel */
        $notifications_model = $this->getContext()->getModel('Notifications', 'Public');
        $notifications_model->notificationNewComment($comment);
            
        $this->setAttribute('approved', $approved);

        return 'Success';
    }
    
    /**
     * Handle validation errors.
     *
     * @param AgaviRequestDataHolder $rd
     * @return mixed Target view spec (string 'ViewName' or array('Module', 'ViewName') )
     */
    /*
    public function handleError(AgaviRequestDataHolder $rd)
    {
        $vm = $this->getContainer()->getValidationManager();
        $errors = $vm->getErrors();
        
        var_dump($errors);
    }
	*/
    
	/**
	 * Returns the default view if the action does not serve the request
	 * method used.
	 *
	 * @return     mixed <ul>
	 *                     <li>A string containing the view name associated
	 *                     with this action; or</li>
	 *                     <li>An array with two indices: the parent module
	 *                     of the view to be executed and the view to be
	 *                     executed.</li>
	 *                   </ul>
	 */
	public function getDefaultViewName()
	{
		return 'Success';
	}
}

?>
